package com.wu.system_service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

/**
 * @ClassName EmailService
 * @Author wuxuan
 * @Date 2019/3/3 19:07
 * @Version 1.0
 * @Description
 **/
@Component
@Configuration
@Slf4j
public class EmailService {

    @Autowired
    JavaMailSender mail;

    @Value("${spring.mail.username}")
    String sender;


    public Boolean sendMail(String Recipient,String title,String content){
        boolean sendSuccess=true;
        SimpleMailMessage message=new SimpleMailMessage();
        message.setFrom(sender);
        message.setTo(Recipient);
        message.setSubject(title);
        message.setText(content);
        try {
            mail.send(message);
            log.info(Recipient+"的邮件发送成功");
        }catch (Exception e){
            log.error(Recipient+"的邮件发送失败");
            sendSuccess=false;
            e.printStackTrace();
        }
        return sendSuccess;
    }
}
